.set LOCALID_RAYQUAZA_SLEEPING, 1

SkyPillar_Top_MapScripts:: @ 82396B8
	map_script MAP_SCRIPT_ON_RESUME, SkyPillar_Top_OnResume
	map_script MAP_SCRIPT_ON_TRANSITION, SkyPillar_Top_OnTransition
	map_script MAP_SCRIPT_ON_WARP_INTO_MAP_TABLE, SkyPillar_Top_OnWarp
	.byte 0

SkyPillar_Top_OnResume: @ 82396C8
	call_if_set FLAG_SYS_CTRL_OBJ_DELETE, SkyPillar_Top_EventScript_TryRemoveRayquaza
	end

SkyPillar_Top_EventScript_TryRemoveRayquaza:: @ 82396D2
	specialvar VAR_RESULT, GetBattleOutcome
	compare VAR_RESULT, B_OUTCOME_CAUGHT
	goto_if_ne Common_EventScript_NopReturn
	removeobject VAR_LAST_TALKED
	return

SkyPillar_Top_OnTransition: @ 82396E6
	compare VAR_SKY_PILLAR_STATE, 2
	call_if_lt SkyPillar_Top_EventScript_SetCleanLayout
	compare VAR_SKY_PILLAR_STATE, 2
	call_if_ge SkyPillar_Top_EventScript_TryShowRayquaza
	end

SkyPillar_Top_EventScript_SetCleanLayout:: @ 82396FD
	setmaplayoutindex LAYOUT_SKY_PILLAR_TOP_CLEAN
	setobjectmovementtype LOCALID_RAYQUAZA_SLEEPING, MOVEMENT_TYPE_FACE_DOWN
	return

SkyPillar_Top_EventScript_TryShowRayquaza:: @ 8239705
	call_if_unset FLAG_DEFEATED_RAYQUAZA, SkyPillar_Top_EventScript_ShowRayquaza
	return

SkyPillar_Top_EventScript_ShowRayquaza:: @ 823970F
	clearflag FLAG_HIDE_RAYQUAZA_SKY_TOWER_SUMMIT
	return

SkyPillar_Top_OnWarp: @ 8239713
	map_script_2 VAR_SKY_PILLAR_STATE, 0, SkyPillar_Top_EventScript_RayquazaFaceDown
	.2byte 0

SkyPillar_Top_EventScript_RayquazaFaceDown:: @ 823971D
	turnobject LOCALID_RAYQUAZA_SLEEPING, DIR_SOUTH
	end

SkyPillar_Top_EventScript_Rayquaza:: @ 8239722
	lockall
	waitse
	playmoncry SPECIES_RAYQUAZA, 2
	delay 40
	waitmoncry
	msgbox SkyPillar_Top_Text_RayquazaSpeechBeforeBattle1, MSGBOX_DEFAULT
	playmoncry SPECIES_RAYQUAZA, 2
	msgbox SkyPillar_Top_Text_RayquazaSpeechBeforeBattle2, MSGBOX_DEFAULT
	@setwildbattle SPECIES_RAYQUAZA, 70, ITEM_NONE
	setlegendarybattle SPECIES_RAYQUAZA, 70, ITEM_NONE, LEGENDARY_MON_RAYQUAZA
	setflag FLAG_SYS_CTRL_OBJ_DELETE
	special BattleSetup_StartLegendaryBattle
	waitstate
	clearflag FLAG_SYS_CTRL_OBJ_DELETE
	specialvar VAR_RESULT, GetBattleOutcome
	@compare VAR_RESULT, B_OUTCOME_WON
	@goto_if_eq SkyPillar_Top_EventScript_DefeatedRayquaza
	compare VAR_RESULT, B_OUTCOME_RAN
	goto_if_eq SkyPillar_Top_EventScript_RanFromRayquaza
	compare VAR_RESULT, B_OUTCOME_PLAYER_TELEPORTED
	goto_if_eq SkyPillar_Top_EventScript_RanFromRayquaza
	setflag FLAG_DEFEATED_RAYQUAZA
	msgbox SkyPillar_Top_Text_RayquazaSpeechAfterBattle, MSGBOX_DEFAULT
	giveitem ITEM_RAYQUAZA_VOUCHER, 1
	closemessage
	setvar VAR_0x8004, SPECIES_RAYQUAZA
	goto Common_EventScript_DefeatLegendary
	@releaseall
	@end

@SkyPillar_Top_EventScript_DefeatedRayquaza:: @ 8239768
@	setflag FLAG_DEFEATED_RAYQUAZA
@	goto SkyPillar_Top_EventScript_DefeatedRayquaza2
@	end

SkyPillar_Top_EventScript_RanFromRayquaza:: @ 8239771
	setvar VAR_0x8004, SPECIES_RAYQUAZA
	goto SkyPillar_Top_EventScript_RanFromRayquaza2
	end

@SkyPillar_Top_EventScript_DefeatedRayquaza2:: @ 823977C
@	fadescreenswapbuffers FADE_TO_BLACK
@	removeobject VAR_LAST_TALKED
@	fadescreenswapbuffers FADE_FROM_BLACK
@	releaseall
@	end

SkyPillar_Top_EventScript_RanFromRayquaza2:: @ 8239785
	fadescreenswapbuffers FADE_TO_BLACK
	removeobject VAR_LAST_TALKED
	fadescreenswapbuffers FADE_FROM_BLACK
	bufferspeciesname 0, VAR_0x8004
	msgbox gText_LegendaryFlewAway, MSGBOX_DEFAULT
	releaseall
	end

SkyPillar_Top_EventScript_AwakenRayquaza:: @ 823979A
	lockall
	fadeoutbgm 1
	applymovement OBJ_EVENT_ID_PLAYER, Common_Movement_FaceUp
	waitmovement 0
	special SpawnCameraObject
	applymovement OBJ_EVENT_ID_CAMERA, SkyPillar_Top_Movement_CameraPanUp
	waitmovement 0
	special RemoveCameraObject
	applymovement LOCALID_RAYQUAZA_SLEEPING, SkyPillar_Top_Movement_RayquazaStir
	waitmovement 0
	waitse
	playmoncry SPECIES_RAYQUAZA, 2
	setvar VAR_0x8004, 1  @ vertical pan
	setvar VAR_0x8005, 1  @ horizontal pan
	setvar VAR_0x8006, 8  @ num shakes
	setvar VAR_0x8007, 3  @ shake delay
	special ShakeCamera
	waitstate
	waitse
	playmoncry SPECIES_RAYQUAZA, 2
	setvar VAR_0x8004, 1  @ vertical pan
	setvar VAR_0x8005, 2  @ horizontal pan
	setvar VAR_0x8006, 8  @ num shakes
	setvar VAR_0x8007, 5  @ shake delay
	special ShakeCamera
	waitstate
	waitmoncry
	applymovement LOCALID_RAYQUAZA_SLEEPING, SkyPillar_Top_Movement_RayquazaFlyOff
	waitmovement 0
	removeobject LOCALID_RAYQUAZA_SLEEPING
	msgbox SkyPillar_Top_Text_RayquazaFlewOff, MSGBOX_DEFAULT
	closemessage
	delay 20
	fadeinbgm 1
	special SpawnCameraObject
	applymovement OBJ_EVENT_ID_CAMERA, SkyPillar_Top_Movement_CameraPanDown
	waitmovement 0
	special RemoveCameraObject
	setvar VAR_SOOTOPOLIS_CITY_STATE, 5
	setvar VAR_SKY_PILLAR_STATE, 1
	setvar VAR_SKY_PILLAR_RAQUAZA_CRY_DONE, 1
	releaseall
	end

SkyPillar_Top_Movement_RayquazaStir: @ 823983A
	delay_16
	walk_in_place_fast_left
	delay_16
	delay_16
	delay_16
	delay_16
	delay_16
	walk_in_place_left
	delay_16
	walk_in_place_right
	delay_16
	delay_16
	delay_16
	delay_16
	delay_16
	delay_16
	step_end

SkyPillar_Top_Movement_RayquazaFlyOff: @ 823984B
	delay_16
	walk_in_place_down
	delay_8
	walk_in_place_right
	delay_8
	walk_fastest_up
	slide_up
	slide_up
	slide_up
	slide_up
	slide_up
	slide_up
	step_end

SkyPillar_Top_Movement_CameraPanUp: @ 8239858
	walk_slow_up
	walk_slow_up
	walk_slow_up
	step_end

SkyPillar_Top_Movement_CameraPanDown: @ 823985C
	walk_slow_down
	walk_slow_down
	walk_slow_down
	step_end

SkyPillar_Top_Text_RayquazaFlewOff: @ 8239860
	.string "The awakened RAYQUAZA flew off…$"

SkyPillar_Top_Text_RayquazaSpeechBeforeBattle1:
	.string "训练家，\n"
	.string "还是回来了吗。\p"
	.string "在超越大地与海洋之上的云层，\n"
	.string "飞跃地球外太空之处，\p"
	.string "那里便是臭氧层。\p"
	.string "无数年，数亿年，\n"
	.string "我在斗争与调解中度过。\p"
	.string "作为无法目测的天空，\n"
	.string "以故事流传的形式，\p"
	.string "于世间生存至今。$"

SkyPillar_Top_Text_RayquazaSpeechBeforeBattle2:
	.string "从你的身上，我能看到，\n"
	.string "无可动摇，坚定不移的信念。\p"
	.string "那时是怎么样的表情，\n"
	.string "此刻也是同样……\p"
	.string "并且在不自知的情况下……\p"
	.string "你也已经上前一步了啊！$"

SkyPillar_Top_Text_RayquazaSpeechAfterBattle:
	.string "比起热烈的勇气，\n"
	.string "还有沉着的思考，\p"
	.string "这些都不重要。\p"
	.string "最初来此处寻找希望的训练家，\n"
	.string "也就是你，果然没有看错。\p"
	.string "我很乐意加入你的队伍，\n"
	.string "不如说这是当时就决定好的想法。\p"
	.string "这份独特的斡旋能力，\n"
	.string "就借给你吧。$"
	
